SgdUsuario = function(){
	var roles;
	return {
		confirmProcess: function(option, sgdUsuarioId, sgdUsuarioActivo){
			Asg.Message.confirm(
					option == "delete" ? Asg.constants.CONFIRM_DELETE_MESSAGE :
						(sgdUsuarioActivo == "1" ? Asg.constants.CONFIRM_INACTIVATE_MESSAGE : Asg.constants.CONFIRM_ACTIVATE_MESSAGE), function(btn){
				if(btn != "no") SgdUsuario.processRegister(option, sgdUsuarioId, sgdUsuarioActivo);
			});
		},
		processRegister: function(option, sgdUsuarioId, sgdUsuarioActivo){
			Asg.Message.progress("Guardarndo Informaci\u00F3n...");
			Ext.Ajax.request({
				timeout: Asg.constants.TIMEOUT_SECONDS,
				url: option == "delete" ? Asg.constants.CONTEXT_PATH + "/admin/usuarios/delete" : Asg.constants.CONTEXT_PATH + "/admin/usuarios/toggle",
				params: {sgdUsuarioId: sgdUsuarioId, sgdUsuarioActivo: sgdUsuarioActivo},
				callback: function(options, success, response){
					Ext.Msg.hide();
					Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "cancel");
					var jsonObject = Asg.Utils.ajaxRequestGetJson(response);
					if(!success || !jsonObject){
						Asg.Message.alertInvalid(Asg.constants.DEFAULT_ERROR_MESSAGE);
						return
					}
					Asg.Message.alert(jsonObject.message || Asg.constants.DEFAULT_ERROR_MESSAGE);
					if(jsonObject.success){
						if(jsonObject.data) 
							Asg.Grid.loadData("sgdUsuarioGrid", jsonObject.data);
						Asg.Form.setEnableAndClear("sgdUsuarioForm", false);
						return;
					}
				}
			});
		},
		loadData: function(record){
			Asg.Message.progress("Cargando Informaci\u00F3n...");
			Ext.Ajax.request({
    			timeout: Asg.constants.TIMEOUT_SECONDS,
  			    url: Asg.constants.CONTEXT_PATH + "/admin/usuarios/findAllSgdRolBySgdUsuarioId",
  			    params: {
  			    	sgdUsuarioId: record.get("sgdUsuario.sgdUsuarioId")
  			    },
  			    callback: function(options, success, response){
  			    	Ext.Msg.hide();
  			    	if(success){
  			    		var jsonObject = Asg.Utils.ajaxRequestGetJson(response);
  			    		if(jsonObject && jsonObject.success){
  			    			Asg.Grid.loadData("sgdRolGrid", jsonObject.sgdRoles);
  			    			Asg.Form.setValues("sgdUsuarioForm", record.data);
    						Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "rowclick");
	        	    		Asg.Form.switchForm("sgdUsuarioForm", "rowclick");
  			    		}else{
  			    			Asg.Message.alertInvalid(jsonObject ? (jsonObject.message || Asg.constants.DEFAULT_ERROR_MESSAGE) : Asg.constants.DEFAULT_ERROR_MESSAGE);
		    				Asg.Grid.clearSelection("sgdUsuarioGrid");
		    				Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "cancel");
	        	    		Asg.Form.switchForm("sgdUsuarioForm", "cancel");
  			    		}
  			    	}
  			    }
  			});
		},
		save: function(){
			var errorMessage = "";
			var sgdUsuarioForm = Ext.getCmp("sgdUsuarioForm");
			if(Asg.Utils.getValue("sgdUsuarioClave") != Asg.Utils.getValue("sgdUsuarioClave1"))
				errorMessage = "* Contrase\u00F1a y Confirmar Contrase\u00F1a deben ser iguales";
			if(!sgdUsuarioForm.getForm().isValid() || !Ext.isEmpty(errorMessage)){
				Asg.Message.alertInvalidFields(errorMessage);
				return;
			}
			var sgdRoles = "[";
			Ext.each(Ext.getCmp("sgdRolGrid").getStore().getRange(), function(){
				if(sgdRoles != "[") sgdRoles += ",{\"sgdRolId\":\"" + this.get("sgdRolId") + "\", " +
						"\"sgdAccesoId\": \"" + (this.get("sgdAccesoId") || 0) + "\",\"sgdAccesoAsignado\":\"" + this.get("sgdAccesoAsignado") +"\"," +
						"\"sgdAccesoPermitirEscribir\":\"" + this.get("sgdAccesoPermitirEscribir") + "\"," +
						"\"sgdAccesoPermitirEliminar\":\"" + this.get("sgdAccesoPermitirEliminar") + "\"}";
				else sgdRoles += "{\"sgdRolId\":\"" + this.get("sgdRolId") + "\", " +
						"\"sgdAccesoId\": \"" + (this.get("sgdAccesoId") || 0) + "\",\"sgdAccesoAsignado\":\"" + this.get("sgdAccesoAsignado") +"\"," +
						"\"sgdAccesoPermitirEscribir\":\"" + this.get("sgdAccesoPermitirEscribir") + "\"," +
						"\"sgdAccesoPermitirEliminar\":\"" + this.get("sgdAccesoPermitirEliminar") + "\"}";
			});
			sgdRoles += "]";
			Asg.Message.progress("Guardando Informaci\u00F3n...");
			Ext.getCmp("sgdUsuarioForm").getForm().submit({
    			url: Asg.constants.CONTEXT_PATH + "/admin/usuarios/save",
    			timeout: Asg.constants.TIMEOUT_SECONDS,
    			params: {
    				"sgdUsuario.sgdUsuarioUsuario": Asg.Utils.getValue("sgdUsuarioUsuario"),
    				sgdRoles: sgdRoles
    			},
    			success: function(form, action){
    				Asg.Message.confirmProcess(action.result.message, function(){
    					if(action.result.success){
    						Asg.Grid.clearSelection("sgdUsuarioGrid");
    						Asg.Utils.setEnable("sgdRolGrid", false);
    						if(action.result.data) 
    							Asg.Grid.loadData("sgdUsuarioGrid", action.result.data);
    						if(!Ext.isEmpty(action.result.dataIndex)){
    							var dataIndex = Ext.getCmp("sgdUsuarioGrid").getStore().find("sgdUsuario.sgdUsuarioId", action.result.dataIndex);
    							Ext.getCmp("sgdUsuarioGrid").getSelectionModel().select(dataIndex);
    						}
    					}
    				});
    			},
    			failure: Asg.Form.failureProcedure
   			});
		},
		getButtonsConfig: function(){
			return {
				add: "sgdUsuarioAddButton",
				edit: "sgdUsuarioEditButton",
				save: "sgdUsuarioSaveButton",
				cancel: "sgdUsuarioCancelButton"
			};
		},
		initConfig: function(config){
			roles = config.roles;
			return {
				items: [
			        {
			        	xtype: "grid",
			        	id: "sgdUsuarioGrid",
			        	frame: true,
			        	margins: "5 5 5 5",
			        	height: 200,
			        	store: new Ext.data.SimpleStore({
			        		data: config.data || [],
			        		fields: [
		        		        "sgdUsuario.sgdUsuarioId", 
		        		        "sgdUsuarioUsuario", 
		        		        "sgdUsuario.sgdUsuarioNombreCompleto", 
		        		        "sgdUsuario.sgdUsuarioCorreo",
		        		        "sgdUsuario.sgdUsuarioTelefono1",
		        		        "sgdUsuario.sgdUsuarioTelefono2",
		        		        "sgdUsuario.sgdUsuarioDireccion",
		        		        "sgdUsuario.sgdUsuarioActivo",
		        		        "sgdUsuario.sgdUsuarioClave",
		        		        "sgdUsuario.sgdUsuarioClave1"
		    		        ]
			        	}),
		        		selModel: {
		        			listeners: {
		        				selectionchange: function(selectionModel, records){
		        					if(records && records.length > 0){
		        						SgdUsuario.loadData(records[0]);
		        					}
		        				}
		        			}
		        		},
			        	columns: [
		        	        {header: "Usuario", dataIndex: "sgdUsuarioUsuario", width: 100},
		        	        {header: "Nombre Completo", dataIndex: "sgdUsuario.sgdUsuarioNombreCompleto", flex: 1, minWidth: 150},  
		        	        {header: "Estado", dataIndex: "sgdUsuario.sgdUsuarioActivo", renderer: "webToActive", width: 100},
		        	        {
		        	        	xtype:"actioncolumn",
		        	        	align: "center",
		        	        	width: 60,
	        	            	items: [
        	            	        {
		        	            		icon: Asg.constants.CONTEXT_PATH + "/resources/images/enableDisable.png",
		        	            		iconCls: "icon",
		        	            		tooltip: "Activar / Inactivar", 
		        	            		handler: function(grid, rowIndex, colIndex) {
		        	                        var record = grid.getStore().getAt(rowIndex);
		        	                        if(record){
		        	                        	SgdUsuario.confirmProcess("toggle", record.get("sgdUsuario.sgdUsuarioId"), record.get("sgdUsuario.sgdUsuarioActivo"));
		        	                        }
		        	            		}
	    	            			},{
		        	            		icon: Asg.constants.CONTEXT_PATH + "/resources/images/delete.png",
		        	            		iconCls: "icon",
		        	            		tooltip: "Eliminar",
		        	            		handler: function(grid, rowIndex, colIndex) {
		        	                        var record = grid.getStore().getAt(rowIndex);
		        	                        if(record){
		        	                        	SgdUsuario.confirmProcess("delete", record.get("sgdUsuario.sgdUsuarioId"), record.get("sgdUsuario.sgdUsuarioActivo"));
		        	                        }
		        	            		}
	    	            			}
    	            	        ]
						    }
			            ],
			        	bbar: [
		        	       "->",
		        	       {
		        	    	   id: "sgdUsuarioAddButton",
		        	    	   text: "Agregar", 
		        	    	   iconCls: Asg.constants.ADD_ICON,
		        	    	   handler: function(){
		        	    		   Asg.Form.setRequired("sgdUsuarioClave", true);
		        	    		   Asg.Form.setRequired("sgdUsuarioClave1", true);
		        	    		   Asg.Form.setAllowEnable("sgdUsuarioUsuario", true);
		        	    		   Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "add");
		        	    		   Asg.Form.switchForm("sgdUsuarioForm", "add");
		        	    		   Asg.Grid.clearSelection("sgdUsuarioGrid");
		        	    		   Asg.Grid.loadData("sgdRolGrid", roles);
		        	    		   Asg.Utils.setEnable("sgdRolGrid", true);
		        	    	   }
	        	    	   },{
	        	    		   id: "sgdUsuarioEditButton",
	        	    		   text: "Editar", 
	        	    		   iconCls: Asg.constants.EDIT_ICON,
	        	    		   hidden: true,
	        	    		   handler: function(){
	        	    			   Asg.Form.setRequired("sgdUsuarioClave", false);
		        	    		   Asg.Form.setRequired("sgdUsuarioClave1", false);
		        	    		   Asg.Form.setAllowEnable("sgdUsuarioUsuario", false);
	        	    			   Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "edit");
	        	    			   Asg.Form.switchForm("sgdUsuarioForm", "edit");
	        	    			   Asg.Utils.setEnable("sgdRolGrid", true);
	        	    		   }
        	    		   },{
        	    			   id: "sgdUsuarioSaveButton",
        	    			   text: "Guardar", 
        	    			   iconCls: Asg.constants.SAVE_ICON,
        	    			   hidden: true,
        	    			   handler: SgdUsuario.save
    	    			   },{
    	    				   id: "sgdUsuarioCancelButton",
    	    				   text: "Cancelar", 
    	    				   iconCls: Asg.constants.CANCEL_ICON,
    	    				   hidden: true,
    	    				   handler: function(){
	        	    			   Asg.Form.switchButton(SgdUsuario.getButtonsConfig(), "cancel");
	        	    			   Asg.Form.switchForm("sgdUsuarioForm", "cancel");
	        	    			   Asg.Grid.clearSelection("sgdUsuarioGrid");
		        	    		   Asg.Grid.loadData("sgdRolGrid", roles);
		        	    		   Asg.Utils.setEnable("sgdRolGrid", false);
	        	    		   }
	    				   }
	        	        ]
			        },{
			        	xtype: "tabpanel",
			        	flex: 1,
			        	minHeight: 200,
			        	margins: "0 5 5 5",
			        	deferredRender: false,
			        	items: [
		        	        {
		        	        	title: "DATOS GENERALES",
		        	        	margin: "5 0 0 0",
		        	        	autoScroll: true,
		        	        	items: {
		        	        		xtype: "form",
		        	        		id: "sgdUsuarioForm",
		        	        		border: false,
		        	        		height: 450,
		        	        		layout: {
			        	        		type: "table",
										columns: 4,
										tableAttrs: {
								            style: {width: "520px"},
								            align: "center"
										}
			        	        	},
			        	        	defaults: Asg.Form.defaults({width: 255, colspan: 2}),
			        	        	items: [
	        	        	        	{xtype: "label", text: "Usuario:", cls: "x-form-item", colspan: 4},
	        	        	        	{
	        	        	        		xtype: "textfield", 
	        	        	        		id: "sgdUsuarioUsuario",
	        	        	        		allowBlank: false,
	        	        	        		maxLength: 20,
	        	        	        		disabled: true,
	        	        	        		maxLength: 20,
	        	        	        		colspan: 4
        	        	        		},
	        	        	        	{xtype: "label", text: "Nombre Completo:", cls: "x-form-item", colspan: 4},
	        	        	        	{
	        	        	        		xtype: "textfield",
	        	        	        		name: "sgdUsuario.sgdUsuarioNombreCompleto",
	        	        	        		allowBlank: false,
	        	        	        		maxLength: 250,
	        	        	        		disabled: true,
	        	        	        		width: 515,
	        	        	        		colspan: 4
        	        	        		},
        	        	        		{xtype: "label", text: "Contrase\u00F1a:", cls: "x-form-item"},
        	        	        		{xtype: "label", text: "Confirmar Contrase\u00F1a:", cls: "x-form-item"},
        	        	        		{
	        	        	        		xtype: "textfield",
	        	        	        		id: "sgdUsuarioClave",
	        	        	        		name: "sgdUsuario.sgdUsuarioClave",
	        	        	        		maxLength: 8,
	        	        	        		inputType: "password",
        	        	        			disabled: true
        	        	        		},{
	        	        	        		xtype: "textfield",
	        	        	        		id: "sgdUsuarioClave1",
	        	        	        		name: "sgdUsuario.sgdUsuarioClave1",
	        	        	        		inputType: "password",
	        	        	        		maxLength: 8,
	        	        	        		disabled: true
        	        	        		},
        	        	        		{xtype: "label", text: "Tel\u00E9fono:", cls: "x-form-item", colspan: 1, width: 130},
        	        	        		{xtype: "label", text: "Celular:", cls: "x-form-item", colspan: 4, width: 125},
        	        	        		{
        	        	        			xtype: "textfield",
        	        	        			id: "sgdUsuarioTelefono1",
        	        	        			name: "sgdUsuario.sgdUsuarioTelefono1",
        	        	        			allowBlank: false,
        	        	        			maxLength: 8,
        	        	        			vtype: "telefono",
        	        	        			disabled: true,
        	        	        			width: 130,
        	        	        			colspan: 1
        	        	        		},{
        	        	        			xtype: "textfield",
        	        	        			id: "sgdUsuarioTelefono2",
        	        	        			maxLength: 8,
        	        	        			name: "sgdUsuario.sgdUsuarioTelefono2",
        	        	        			vtype: "telefonoCelular",
        	        	        			disabled: true,
        	        	        			width: 125,
        	        	        			colspan: 4
        	        	        		},
        	        	        		{xtype: "label", text: "Correo Electr\u00F3nico:", cls: "x-form-item", colspan: 4},
        	        	        		{
        	        	        			xtype: "textfield",
        	        	        			id: "sgdUsuarioCorreo",
        	        	        			name: "sgdUsuario.sgdUsuarioCorreo",
        	        	        			maxLength: 250,
        	        	        			vtype: "email",
        	        	        			disabled: true,
        	        	        			width: 515,
        	        	        			colspan: 4
        	        	        		},
        	        	        		{xtype: "label", text: "Direcci\u00F3n:", cls: "x-form-item", colspan: 4},
        	        	        		{
        	        	        			xtype: "textarea",
        	        	        			id: "sgdUsuarioDireccion",
        	        	        			name: "sgdUsuario.sgdUsuarioDireccion",
        	        	        			maxLength: 300,
        	        	        			disabled: true,
        	        	        			width: 515,
        	        	        			height: 40,
        	        	        			colspan: 4
        	        	        		},{
        	        	        			xtype: "hidden",
        	        	        			name: "sgdUsuario.sgdUsuarioId"
        	        	        		},{
        	        	        			xtype: "hidden",
        	        	        			name: "sgdUsuario.sgdUsuarioActivo",
        	        	        			defaultValue: "1"
        	        	        		}
	        	        	        ]
		        	        	}
		        	        },{
		        	        	title: "ACCESOS",
		        	        	layout: {
									type: "vbox",
									align : "center",
									pack  : "start"
								},
		        	        	items: {
			        	        	xtype: "grid",
			        	        	id: "sgdRolGrid",
						        	margins: "5 5 5 5",
						        	flex: 1,
						        	minHeight: 200,
						        	frame: true,
						        	width: 600,
						        	disabled: true,
						        	store: new Ext.data.SimpleStore({
						        		data: config.roles || [],
						        		fields: [
					        		        "sgdRolId",
					        		        "sgdRolNombre",
					        		        "sgdAccesoAsignado",
					        		        "sgdAccesoPermitirEscribir",
					        		        "sgdAccesoPermitirEliminar",
					        		        "sgdAccesoId"
					    		        ]
						        	}),
						        	columns: [
				        	            {
				        	            	xtype:"checkcolumn",
				        	            	align: "center",
				        	            	dataIndex: "sgdAccesoAsignado", 
				        	            	width: 30
			        	            	},  
					        	        {header: "ACCESO", dataIndex: "sgdRolNombre", flex: 1},
					        	        {
				        	            	xtype:"checkcolumn",
				        	            	header: "Permitir Agregar / Actualizar",
				        	            	align: "center",
				        	            	dataIndex: "sgdAccesoPermitirEscribir",
				        	            	width: 150
			        	            	},{
				        	            	xtype:"checkcolumn",
				        	            	header: "Permitir Eliminar",
				        	            	align: "center",
				        	            	dataIndex: "sgdAccesoPermitirEliminar",
				        	            	width: 150
			        	            	}
						            ]
			        	        }
		        	        }
	        	        ]
			        }
		        ]
			};
		}
	};
}();